package com.eascs.web.o2o.web.service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.scheduler.common.entity.ScheduleJob;
import com.eascs.web.o2o.web.dao.QuartzDao;
import com.eascs.web.o2o.web.form.ScheduleJobForm;
import com.eascs.web.sso.UserHolder;

import org.springframework.stereotype.Service;

import javax.inject.Inject;

import java.util.Date;

@Service
public class QuartzService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Inject
	QuartzDao baseDao;
	
	public boolean updateEntity(ScheduleJob job){
		return baseDao.updateEntity(job);
	}

	public boolean insertEntity(ScheduleJob job){
		return baseDao.insert(job);
	}
	
	public boolean deleteEntityById(String id){
		return baseDao.delete(ScheduleJob.class, id);
	}
	
	public ScheduleJob getJobByName(String annotionName,String methodName){
		return (ScheduleJob) baseDao.findEntity("select * from schedule_job where class_path =? and method=?", ScheduleJob.class, annotionName,methodName);
	}
	
	public ScheduleJob getJobByJobName(String jobName){
		return (ScheduleJob) baseDao.findEntity("select * from schedule_job where job_name  =?", ScheduleJob.class, jobName);
	}
	
	public ScheduleJob getJobById(String jobId){
		return (ScheduleJob) baseDao.findEntity("select * from schedule_job where job_id  =?", ScheduleJob.class, jobId);
	}
	
	public PageResult<Object[]> findScheduleJobInfo(ScheduleJobForm vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuilder sql = new StringBuilder();
		sql.append("select t.job_id,t.job_name,t.class_path, t.method,t.cron_expression,case when t.job_status='0' then '暂停中' else '运行' end,t.update_time,t.start_time,t.end_time,t.priority,t.remark from schedule_job t  where 1=1 ");
		//请求系统编码
		if (StringUtil.isNotBlank(vo.getJobName())) {
			sql.append(" and t.job_name = ?");
			sqlPageQuery.addParam(vo.getJobName());
		}
		if (StringUtil.isNotBlank(vo.getClassPath())) {
			sql.append(" and t.class_path = ?");
			sqlPageQuery.addParam(vo.getClassPath());
		}
		if (StringUtil.isNotBlank(vo.getMethod())) {
			sql.append(" and t.method = ?");
			sqlPageQuery.addParam(vo.getMethod());
		}
		sql.append(" order by t.create_time desc ");
  		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> customerBasepage = baseDao.findDataWithPage(sqlPageQuery);
		return customerBasepage;
	}
	
	public ScheduleJob saveScheduleJob(ScheduleJobForm vo,ScheduleJob j) {
		ScheduleJob job = null;
		if(("update").equals(vo.getFlag())){
			job = j;
			job.setCronExpression(vo.getCronExpression());
			job.setPriority(vo.getPriority());
			job.setRemark(vo.getRemark());
			job.setUpdateTime(new Date());
			job.setUpdatePsn(UserHolder.getUser().getCname());
		}else{
			job = new ScheduleJob();
			job.setJobName(vo.getJobName());
			job.setTriggerName("trigger_"+vo.getJobName());
			job.setClassPath(vo.getClassPath());
			job.setMethod(vo.getMethod());
			job.setCronExpression(vo.getCronExpression());
			job.setPriority(vo.getPriority());
			job.setRemark(vo.getRemark());
			job.setJobStatus("0");//新建时为暂停状态
			//job.setIsScucess("1");
			job.setConcurrent("0");//不并发
			job.setIsSpringbean("1");//是springbean
			job.setCreateTime(new Date());
			job.setCreatePsn(UserHolder.getUser().getCname());
		}
		return job;
	}
	
}
